#include "StdAfx.h"
#include "TinyUser.h"
#include "TinyServerDefine.h"
#include "UserMapper.h"

using namespace DBLayer;
namespace RuntimeMgr{

CTinyUser::CTinyUser(void)
{
}

CTinyUser::~CTinyUser(void)
{
}
}
bool RuntimeMgr::CTinyUser::initByMsg(CTinyMsg& inMsg)
{	
	m_socketID = inMsg.getSocketID();
	inMsg.getDefaultParamByIndex(CLIENTLOGON_MODULENAME,m_ModuleName);
	inMsg.getDefaultParamByIndex(CLIENTLOGON_MODULETYPE,m_ModuleType);
	inMsg.getDefaultParamByIndex(CLIENTLOGON_USERNAME,m_Name);
	//inMsg.getDefaultParamByIndex(CLIENTLOGON_PASSWORD,password);
	inMsg.getDefaultParamByIndex(CLIENTLOGON_REGION,m_Region);

	return true;
}

bool RuntimeMgr::CTinyUser::ProcessReceivedMsg(CTinyMsg& inMsg,CTinyMsg& outMsg)
{
	bool bRet = false;

	if(inMsg.getMsgIndex() == CLIENT_ACTION)
	{
		switch (inMsg.getMsgItem())
		{
		case CLIENT_FRIENDLIST_REQUEST:
			{
				//CTinyDBUserMapper* usermapper = dynamic_cast<CTinyDBUserMapper*>(g_TinyCCDBMgr.findMapper(DB_USERINFO));
				//if(usermapper!=NULL)
				{
					int dpc = inMsg.getDefaultParamCount();
					string mname,mtype,username,password,region;
					inMsg.getDefaultParamByIndex(CLIENTFRIENDLIST_MODULENAME,mname);
					inMsg.getDefaultParamByIndex(CLIENTFRIENDLIST_MODULETYPE,mtype);
					inMsg.getDefaultParamByIndex(CLIENTFRIENDLIST_USERNAME,username);
					inMsg.getDefaultParamByIndex(CLIENTFRIENDLIST_REGION,region);

					outMsg.setMsgIndex(CLIENT_ACTION);
					outMsg.setMsgItem(CLIENT_FRIENDLIST_SUCC);

					std::vector<std::string> friends = g_TinyCCDBMgr.users->getFriends(username);

					if(friends.size()<=0)
					{
						bRet = false;
						break;
					}
					//push friends vector;
					std::string friendslist = "";
					for(int i =0;i<(int)friends.size();++i)
					{
						if(friendslist != "")
							friendslist +=",";
						if(friends[i].substr(0,friends[i].find(":"))!=m_Name)
							friendslist +=friends[i];
					}
					outMsg.setDefaultParam(CLIENTFRIENDLISTSUCC_MODULENAME,mname);
					outMsg.setDefaultParam(CLIENTFRIENDLISTSUCC_MODULETYPE,mtype);
					outMsg.setDefaultParam(CLIENTFRIENDLISTSUCC_USERNAME,username);
					outMsg.setDefaultParam(CLIENTFRIENDLISTSUCC_REGION,region);
					outMsg.setDefaultParam(CLIENTFRIENDLISTSUCC_FRIENDLIST,friendslist);
				}//end if NULL
				bRet = false;
				break;
			}//end if CLIENT_FRIENDLIST_REQUEST
			break;
		case CLIENT_SEND_TXTMSG:
			{
				int dpc = inMsg.getDefaultParamCount();
				string mname,mtype,username,password,region,target,content;
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MODULENAME,mname);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MODULETYPE,mtype);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_USERNAME,username);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_REGION,region);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_TARGET, target);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MSG, content);
				
				//if user has logoned ,send msg
				CTinyUser* user = g_TinyCCClientMgr.getUserByName(username);
				CTinyUser* targetUser = g_TinyCCClientMgr.getUserByName(target);
				if(user != NULL && targetUser != NULL)
				{
					outMsg.setMsgIndex(CLIENT_ACTION);
					outMsg.setMsgItem(CLIENT_SEND_TXTMSG_WAIT);
					//send msg here
					
					CTinyMsg txtMsg;
					txtMsg.setMsgIndex(CLIENT_ACTION);
					txtMsg.setMsgItem(CLIENT_SEND_TXTMSG);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MODULENAME,mname);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MODULETYPE,mtype);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_USERNAME,username);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_REGION,region);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_TARGET,target);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MSG,content);
					//g_TinyTCPIF.sendMsg(user->m_socketID,txtMsg);
					g_TinyTCPIF.sendMsg(targetUser->m_socketID,txtMsg);
				}
				else
				{
					outMsg.setMsgIndex(CLIENT_ACTION);
					outMsg.setMsgItem(CLIENT_SEND_TXTMSG_ERROR);
				}
			}
			break;
		case CLIENT_SEND_TXTMSG_SUCC:
			{
				int dpc = inMsg.getDefaultParamCount();
				string mname,mtype,username,password,region,target,content;
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MODULENAME,mname);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MODULETYPE,mtype);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_USERNAME,username);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_REGION,region);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_TARGET, target);
				inMsg.getDefaultParamByIndex(CLIENT_SEND_TXTMSG_MSG, content);

				//if user has logoned ,send msg
				CTinyUser* user = g_TinyCCClientMgr.getUserByName(username);
				if(user != NULL)
				{
					CTinyMsg txtMsg;
					txtMsg.setMsgIndex(CLIENT_ACTION);
					txtMsg.setMsgItem(CLIENT_SEND_TXTMSG_FINISHED);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MODULENAME,mname);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MODULETYPE,mtype);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_USERNAME,username);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_REGION,region);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_TARGET,target);
					txtMsg.setDefaultParam(CLIENT_SEND_TXTMSG_MSG,content);
					g_TinyTCPIF.sendMsg(user->m_socketID,txtMsg);
				}
			}break;
		default:
			break;
		}
	}

	return false;
}
int RuntimeMgr::CTinyUser::getSocketID(void)
{
	return m_socketID;
}
